! 函数作为函数指针变量传递
module func_pointer

    implicit none
    procedure(fcn), pointer :: op => null() ! 函数指针变量实例

    abstract interface                      ! 抽象接口
        pure subroutine fcn(x, y, z)
            integer, intent(in) :: x, y
            integer, intent(out) :: z
        end subroutine fcn
    end interface

contains

    pure subroutine add(x, y, z)
        integer, intent(in) :: x, y
        integer, intent(out) :: z
        z = x + y
    end subroutine add

    pure subroutine subtract(x, y, z)
        integer, intent(in) :: x, y
        integer, intent(out) :: z
        z = x - y
    end subroutine subtract

end module func_pointer

program main

    use func_pointer
    implicit none

    integer :: x, y, z

    x = 1; y = 2
    op => add                               ! 函数指针变量指向具体函数
    call op(x, y, z)
    print *, z                              ! 3

    op => subtract                          ! 函数指针变量指向具体函数
    call op(x, y, z)
    print *, z                              ! -1

end program main
